local blaze = require "blaze"
local app = require "app"
local game = app.game.running
local consts = game.util.consts

local M = game.ui.element()

local function update_tip(self, state)
    -- 暂时不需要显示这个阶段提示了
    -- if state.phase_id == consts.PHASE_IDLE then
    --     self.tip_txt.text = string.format("未准备 请准备下注")
    --     return
    -- end

    -- local countdown = math.floor(state.countdown)
    -- countdown = countdown <= 0 and " " or tostring(countdown)

    -- if state.phase_id == consts.PHASE_BET then
    --     self.tip_txt.text = string.format("下注 请下注%s", countdown)
    --     return
    -- end

    -- if state.phase_id == consts.PHASE_REVEAL then
    --     self.tip_txt.text = string.format("结果 停止下注%s", countdown)
    --     return
    -- end

    -- if state.phase_id == consts.PHASE_SETTLE then
    --     self.tip_txt.text = string.format("结算中 准备派彩%s", countdown)
    --     return
    -- end

    -- if state.phase_id == consts.PHASE_PAYOUT then
    --     self.tip_txt.text = string.format("派彩 准备下一局%s", countdown)
    -- end
end

local function loop(self)
    local state = game.store.gameplay.state
    while true do
        update_tip(self, state)
        game.coroutine.wait(1)
    end
end

M.store_watchers = {
    gameplay = {
        ["$"] = blaze.noop,

        ["阶段变更"] = function(self, store, state, args)
            if state.phase_id == consts.PHASE_IDLE then
                self.start_bet:SetActive(false)
                self.stop_bet:SetActive(false)
            elseif state.phase_id == consts.PHASE_BET then
                game.audio.post("开始下注")
                self.start_bet:SetActive(true)
                self.stop_bet:SetActive(false)
            elseif state.phase_id == consts.PHASE_REVEAL then
                game.audio.post("停止下注")
                self.start_bet:SetActive(false)
                self.stop_bet:SetActive(true)
            elseif state.phase_id == consts.PHASE_SETTLE then
                self.start_bet:SetActive(false)
                self.stop_bet:SetActive(false)
            elseif state.phase_id == consts.PHASE_PAYOUT then
                self.start_bet:SetActive(false)
                self.stop_bet:SetActive(false)
            end
            update_tip(self, state)
        end
    }
}

function M:ctor()
    self.res = "club.threecard.baccaratlib/ui/common.b:phase_shower"
end

function M:on_injected(bind)
    self:start_coroutine(loop, self)
end

return M